package com.example.example5;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	Button btn1,btn2,btn3;
	Button btn4,btn5,btn6;
	Button btn7,btn8,btn9;
	Button btn0,btnClear,btnStar;
	TextView viewNum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btn1= (Button) findViewById(R.id.button1);
        btn2= (Button) findViewById(R.id.button2);
        btn3= (Button) findViewById(R.id.button3);
        btn4= (Button) findViewById(R.id.button4);
        btn5= (Button) findViewById(R.id.button5);
        btn6= (Button) findViewById(R.id.button6);
        btn7= (Button) findViewById(R.id.button7);
        btn8= (Button) findViewById(R.id.button8);
        btn9= (Button) findViewById(R.id.button9);
        btn0= (Button) findViewById(R.id.button0);
        btnClear= (Button) findViewById(R.id.button_clear);
        btnStar= (Button) findViewById(R.id.button_star);
        viewNum= (TextView) findViewById(R.id.number_display);
        
        btn1.setOnClickListener(appendString("1"));
        btn2.setOnClickListener(appendString("2"));
        btn3.setOnClickListener(appendString("3"));
        btn4.setOnClickListener(appendString("4"));
        btn5.setOnClickListener(appendString("5"));
        btn6.setOnClickListener(appendString("6"));
        btn7.setOnClickListener(appendString("7"));
        btn8.setOnClickListener(appendString("8"));
        btn9.setOnClickListener(appendString("9"));
        btn0.setOnClickListener(appendString("0"));
        btnStar.setOnClickListener(appendString("*"));
        
        btnClear.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				viewNum.setText("");
				
			}
		});
    }
    
    
	private OnClickListener appendString(final String para) {
		return new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				viewNum.append(para);
			}
		};
		
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        
        menu.add(0,Menu.FIRST,0,"EXIT").setIcon(android.R.drawable.ic_menu_delete);
        
        return true;
    }


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId())
		{
			case Menu.FIRST:
			{
				Toast t=Toast.makeText(MainActivity.this, "EXIT.....! See u again", Toast.LENGTH_LONG);
			
				t.show();
				finish();
			}
		}
		return super.onOptionsItemSelected(item);
	}
    
    
}
